package com.authentications.utils;

import com.authentications.security.info.SecurityValue;
import io.jsonwebtoken.*;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName JwtSpringSecurityTokenUtils
 * @Author xiezhuocai
 * @Description SpringSecurity的jwt工具
 * @Date 2021/12/20 8:40
 */
public class JwtSpringSecurityTokenUtils {
    public static final String TOKEN_HEADER = "Authorization";
    public static final String TOKEN_PREFIX = "Bearer ";
    public static String SECRET = "admin";
    public static String ISS = "echisan";
    private static Long EXPIRATION = 60 * 60 * 3L; //过期时间3小时
    private static String ROLE = "role";

    public JwtSpringSecurityTokenUtils(SecurityValue securityValue){
        if (!StringUtils.isEmpty(securityValue.getSigning_key())){
            SECRET = securityValue.getSigning_key();
        }

        if (!StringUtils.isEmpty(securityValue.getIss())){
            ISS = securityValue.getIss();
        }

        if (securityValue.getExpiraton()!=null){
            EXPIRATION = securityValue.getExpiraton();
        }

        if (!StringUtils.isEmpty(securityValue.getRole())){
            ROLE = securityValue.getRole();
        }
    }

    //创建token
    public String createToken(String username, List<String> role, boolean isRememberMe){
        Map map = new HashMap();
        map.put(ROLE, role);
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .setClaims(map)
                .setIssuer(ISS)
                .setSubject(username)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION * 1000))
                .compact();
    }

    //从token中获取用户名(此处的token是指去掉前缀之后的)
    public String getUserName(String token){
        String username;
        try {
            username = getTokenBody(token).getSubject();
        } catch (    Exception e){
            username = null;
        }
        return username;
    }

    public List<String> getUserRole(String token){
        return (List<String>) getTokenBody(token).get(ROLE);
    }

    private Claims getTokenBody(String token){
        Claims claims = null;
        try{
            claims = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
        } catch(ExpiredJwtException e){
            e.printStackTrace();
        } catch(UnsupportedJwtException e){
            e.printStackTrace();
        } catch(MalformedJwtException e){
            e.printStackTrace();
        } catch(SignatureException e){
            e.printStackTrace();
        } catch(IllegalArgumentException e){
            e.printStackTrace();
        }
        return claims;
    }

    //是否已过期
    public boolean isExpiration(String token){
        try{
            return getTokenBody(token).getExpiration().before(new Date());
        } catch(Exception e){
            System.out.println(e.getMessage());
        }
        return true;
    }

    // 从token中获取用户名
    public String getUsername(String token){
        return getTokenBody(token).getSubject();
    }

}
